// components/GBTopTap/GBTopTap.js
const util = require("../../utils/util.js");
const getdata = require("../../tool/GetDate.js");
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    hallTitle: {
      type: String,
      value: "",
    },
    mealTitle: {
      type: String,
      value: "",
    },
    dataTitle: {
      type: String,
      value: "",
    },
    districtTitle: {
      type: String,
      value: "",
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    currentTab: 0,
    isStatus: "",
    isItemOpen: false,
    startDate: "",
    endDate: "",
    hallTitle: "",
    mealTitle: "",
    dataTitle: "",
    diningHallArray: [],
    objectDiningHallArray: [],
    index: 0,
    mealArray: [],
    objectMealArray: [],
    mealindex: 0,
    districtTitle: "",
    districtArray: [],
    objectDistrictArray: [],
    districtIndex: 0,
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      let that = this;
      if (that.data.districtTitle) {
        getdata.getdata({
          url: "gbt/BasicInfo/DistrictInfo/",
          data: app.globalData.myUserData.districtID,
          success: function (res) {
            console.log(res.root);
            if (res.root) {
              // let diningHallIndex = wx.getStorageSync("diningHallPickerIndex");
              // if (!diningHallIndex) {
              //   diningHallIndex = 0;
              // }

              that.setData({
                districtArray: res.root.districtTitleArray,
                objectDistrictArray: res.root.districtArray,
                // index: diningHallIndex,
              });
            }
          },
        });
      }
      if (that.data.hallTitle) {
        getdata.getdata({
          url: "gbt/BasicInfo/DiningHall/",
          data: app.globalData.myUserData.districtID,
          success: function (res) {
            console.log(res.root);
            if (res.root) {
              // let diningHallIndex = wx.getStorageSync("diningHallPickerIndex");
              // if (!diningHallIndex) {
              //   diningHallIndex = 0;
              // }

              that.setData({
                diningHallArray: res.root.diningHallTitleArray,
                objectDiningHallArray: res.root.diningHallArray,
                // index: diningHallIndex,
              });
            }
          },
        });
      }
      if (that.data.mealTitle) {
        getdata.getdata({
          url: "gbt/BasicInfo/MealList/",
          data: app.globalData.myUserData.districtID,
          success: function (res) {
            console.log(res.root);
            if (res.root) {
              let array = [];
              for (let mealIndex = 0; mealIndex < res.root.data.length; mealIndex++) {
                array.push(res.root.data[mealIndex].name);
              }

              that.setData({
                mealArray: array,
                objectMealArray: res.root.data,
              });
            }
          }
        });
      }
    },
    hide: function () {},
    resize: function () {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onMask: function () {
      this.setData({
        isItemOpen: false,
      });
    },

    swichNav: function (e) {
      console.log(e.target.dataset);
      if (e.target.dataset.current === "0") {
        let param = {};
        param.userID = app.globalData.myUserData.userID;
        param.hallID = this.data.objectDiningHallArray[this.data.index].id;
        param.districtID_base = app.globalData.myUserData.districtID;
        param.hallName = "";
        param.mealName = "";
        param.startDate = util.formatDateDiff(-7);
        param.endDate = util.formatDate(new Date);
        param.currentPage = 1;
        param.districtID = app.globalData.myUserData.districtID;
        let data = JSON.stringify(param);
        this.triggerEvent('def', data);
        this.setData({
          currentTab: parseInt(e.target.dataset.current),
          isItemOpen: false,
        });
        return;
      }
      var that = this;
      that.setData({
        currentTab: parseInt(e.target.dataset.current),
        isItemOpen: true,
      });
    },

    bindStartDateChange: function (e) {
      this.setData({
        startDate: e.detail.value,
      });
    },

    bindEndDateChange: function (e) {
      this.setData({
        endDate: e.detail.value,
      });
    },

    onDateConfirm: function (e) {
      console.log(e.currentTarget.dataset);
      let param = {};
      param.userID = app.globalData.myUserData.userID;
      param.hallID = this.data.objectDiningHallArray[this.data.index].id;
      if (this.data.districtTitle) {
        param.districtID_base = this.data.objectDistrictArray[this.data.districtIndex].id;
      }
      param.hallName = e.currentTarget.dataset.hall;
      param.mealName = e.currentTarget.dataset.meal;
      param.currentPage = 1;
      param.districtID = app.globalData.myUserData.districtID;
      if (e.currentTarget.dataset.startdate) {
        param.startDate = e.currentTarget.dataset.startdate;
      } else {
        param.startDate = util.formatDateDiff(-7);
      }
      if (e.currentTarget.dataset.enddate) {
        param.endDate = e.currentTarget.dataset.enddate;
      } else {
        param.endDate = util.formatDate(new Date);
      }
      console.log(param);
      let data = JSON.stringify(param);
      this.triggerEvent('dateconfirm', data);
      this.setData({
        isItemOpen: false,
      });
    },

    onGetDataConfirm: function (e) {
      let data = this.data.objectDistrictArray[this.data.districtIndex].id;
      // console.log(data);
      this.triggerEvent('dataconfirm', data);
      this.setData({
        isItemOpen: false,
      });
    },

    bindDistrictPickerChange: function (e) {
      this.setData({
        districtIndex: e.detail.value,
      });
    },

    bindDiningHallPickerChange: function (e) {
      this.setData({
        index: e.detail.value,
      });
    },

    bindMealPickerChange: function (e) {
      this.setData({
        mealindex: e.detail.value,
      });
    },

    doNotMove: function () {
      return;
    }
  }
})